﻿using WCell.Util.Commands;
using WCell.RealmServer.Addons;
using WCell.Core.Addons;

namespace WCell.RealmServer.Commands
{
    public class AddonCommand : RealmServerCommand
    {
        protected override void Initialize()
        {
            Init("Addon");
            EnglishDescription = "Provides commands for managing Addons";
        }

        public class ListAddonsCommand : SubCommand
        {
            protected override void Initialize()
            {
                Init("List", "L");
                EnglishDescription = "Lists all active Addons.";
            }

            public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
            {
                var i = 0;
                foreach (var context in WCellAddonMgr.Contexts.Values)
                {
                    ++i;
                    if (context.Addon != null)
                    {
                        trigger.Reply(i + ". Addon " + context.Addon);
                    }
                    else
                    {
                        trigger.Reply(i + ". Library " + context.Assembly);
                    }
                }
            }
        }

        public class LoadAddonCommand : SubCommand
        {
            protected override void Initialize()
            {
                Init("Load");
                ParamInfo = "[-c <Classname>] <Path>";
                EnglishDescription = "Loads a new Addon. The parameter is equal to Addon descriptions in the config.";
            }

            public override void Process(CmdTrigger<RealmServerCmdArgs> trigger)
            {
                var mod = trigger.Text.NextModifiers();
                string className;
                if (mod == "c")
                {
                    className = trigger.Text.NextWord();
                }
                else
                {
                    className = null;
                }

                var path = trigger.Text.NextWord();
                if (path.Length == 0)
                {
                    trigger.Reply("No Path given.");
                }
                else
                {
                    var desc = new WCellAddonDescriptor(path, className);
                    trigger.Reply("Loading addon from " + path + "...");
                    RealmAddonMgr.Load(desc);
                    trigger.Reply("Done: " + desc);
                }
            }
        }
    }
}
